/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import engine.linkList.LinkedListManager;
import engine.linkList.LocalToScanFolderNode;
import engine.linkList.MusicListNode;
import engine.linkList.Node;
import engine.linkList.PlayScheduleManager;
import engine.player.MusicPlayer;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 *用于初始化UI
 * @author hjp
 */
public class InitMusicPlayerUI {
    
    private InitMusicPlayerUI(){
        
    }
    
    private static InitMusicPlayerUI initMusicPlayerUI = new InitMusicPlayerUI();
    
    public static InitMusicPlayerUI getInstance(){
        return initMusicPlayerUI;
    }
    
    public void initUI(){
        //初始化的时候，默认显示的是本地歌单
        MusicPlayerUI.getInstance().printMusicList(MusicPlayerUI.getInstance().getMusicListTabel());
        //初始化createMusicListsPanel
        Node<MusicListNode> p = LinkedListManager.getInstance().getCreateMusicLists().head;
        while(p != null){
                JButton newButton = new JButton(p.info.toString() + "            ");
                newButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
                newButton.setFont((new java.awt.Font("楷体", 0, 32)));
                newButton.addMouseListener(new MouseAdapter(){
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            //屏蔽选择目录以及匹配音乐按钮
                            MusicPlayerUI.getInstance().getAddLocalToScanFolderNodeButton().setVisible(false);
                            MusicPlayerUI.getInstance().getMatchMusicButton().setVisible(false);
                            //设置currentVisitMusicListLabel
                            MusicPlayerUI.getInstance().getCurrentVisitMusicListLabel().setText(newButton.getText().trim());
                            //修改当前访问的链表集合
                            LinkedListManager.getInstance().setCurrentVisitMusicLists(LinkedListManager.getInstance().getCreateMusicLists());
                            //修改当前访问的链表
                            LinkedListManager.getInstance().setCurrentVisitMusicList(LinkedListManager.getInstance().findSetectedMusicList(LinkedListManager.getInstance().getCreateMusicLists(), newButton.getText().trim()));
                            //为中间变量赋值
                            MusicPlayerUI.getInstance().transientCurrentVisitMusicLists = LinkedListManager.getInstance().getCurrentVisitMusicLists();
                            MusicPlayerUI.getInstance().transientCurrentVisitMusicList = LinkedListManager.getInstance().getCurrentVisitMusicList();
                            //将该链表的内容送入musicListTabel中
                            MusicPlayerUI.getInstance().printMusicList(MusicPlayerUI.getInstance().getMusicListTabel());
                            //如果CardLayout当前显示的是card2，则强行返回card1
                            CardLayout card = (CardLayout)MusicPlayerUI.getInstance().getListPanel().getLayout();
                            card.show(MusicPlayerUI.getInstance().getListPanel(), "card1");
                            //置当前访问card指针为card1
                            MusicPlayerUI.getInstance().currentCard = "card1";
                        }
                });
                newButton.setBackground(new Color(250,250,250));
                JPanel panel  = MusicPlayerUI.getInstance().getCreateMusicListsPanel();
                panel.add(newButton);
                panel.revalidate();
                p = p.next;
        }
        //初始化播放模式按钮
        Icon icon = null;
        switch(PlayScheduleManager.getInstance().getCurrentMode()){
            case SEQUENCE:{
                icon = new ImageIcon(getClass().getResource("/GUI/icons/sequence.png"));
                MusicPlayerUI.getInstance().getPlayModeButton().setToolTipText("顺序播放");
                MusicPlayerUI.playModeButtonClickedCount = 0;
                break;
            }
            case RANDOM:{
                icon = new ImageIcon(getClass().getResource("/GUI/icons/random.png"));
                MusicPlayerUI.getInstance().getPlayModeButton().setToolTipText("随机播放");
                MusicPlayerUI.playModeButtonClickedCount = 1;
                break;
            }
            case SINGLE_CYCLE:{
                icon = new ImageIcon(getClass().getResource("/GUI/icons/singleCycle.png"));
                MusicPlayerUI.getInstance().getPlayModeButton().setToolTipText("单曲循环");
                MusicPlayerUI.playModeButtonClickedCount = 2;
                break;
            }
        }
        MusicPlayerUI.getInstance().getPlayModeButton().setIcon(icon);
        //初始化musicNameLabel
        if (PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy != null) {
            MusicPlayerUI.getInstance().getMusicNameLabel().setText(PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy.info.getSongName());
        }
        else{
            MusicPlayerUI.getInstance().getMusicNameLabel().setText("音乐，你的生活！");
        }
//        //初始化currentPlayTimeJLabel
//        if (PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy != null) {
//            String totalTime = PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy.info.getTime_String();
//            //格式化当前播放时间
//            String minute = null;
//            String seconds = null;
//            if ((MusicPlayer.currentPlayTime / 60) < 10) {
//                minute = "0" + String.valueOf(MusicPlayer.currentPlayTime / 60);
//            }
//            else{
//                minute =  String.valueOf(MusicPlayer.currentPlayTime / 60);
//            }
//            if ((MusicPlayer.currentPlayTime % 60) < 10) {
//                seconds = "0" + String.valueOf(MusicPlayer.currentPlayTime % 60);
//            }
//            else{
//                seconds =  String.valueOf(MusicPlayer.currentPlayTime % 60);
//            }
//            MusicPlayerUI.getInstance().getCurrentPlayTimeJLabel().setText(minute + ":" + seconds + "/" + totalTime);
//        }
//        else{
//            MusicPlayerUI.getInstance().getCurrentPlayTimeJLabel().setText("00:00/00:00");
//        }
        if (PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy != null) {
            MusicPlayerUI.getInstance().getCurrentPlayTimeJLabel().setText("00:00" + "/" + PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy.info.getTime_String());
        }
        else{
            MusicPlayerUI.getInstance().getCurrentPlayTimeJLabel().setText("00:00/00:00");
        }
        
        //初始化currentPlayTimeJSlider
        if (PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy != null) {
            MusicPlayerUI.getInstance().getCurrentPlayTimeJSlider().setMaximum(PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy.info.getTime_Int());
            //MusicPlayerUI.getInstance().getCurrentPlayTimeJSlider().setValue(MusicPlayer.currentPlayTime);
            MusicPlayerUI.getInstance().getCurrentPlayTimeJSlider().setValue(0);
        }
        else{
            MusicPlayerUI.getInstance().getCurrentPlayTimeJSlider().setValue(0);
        }
        //初始化ChooseLocalToScanFolderDialog
        Node<LocalToScanFolderNode> pLocalToScanFolderNode = LinkedListManager.getInstance().getLocalToScanFolderList().head;
        while(pLocalToScanFolderNode != null){
                    //更新ChooseLocalToScanFolderDialog的UI
//                    try {
//                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//                  } catch (Exception e1) {
//                   e1.printStackTrace();
//                  }
                    String path = pLocalToScanFolderNode.info.path;
                    JCheckBox newCheckBox = new JCheckBox(path);
                    newCheckBox.setFont(new java.awt.Font("楷体", 0, 24));
                    //添加监听器
                    newCheckBox.addMouseListener(new MouseAdapter(){
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            Node<LocalToScanFolderNode> selectedLocalToScanFolderNode = LinkedListManager.getInstance().findSelectedLocalToScanFolderNode(path);
                            selectedLocalToScanFolderNode.info.isSelect = newCheckBox.isSelected();
                        }
                    });
                    //新创建的带扫描的文件夹结点当然默认是已选中的
                    newCheckBox.setSelected(pLocalToScanFolderNode.info.isSelect);
                    MusicPlayerUI.getInstance().dialog.getLocalToScanFolderListPanel().add(newCheckBox);
                    MusicPlayerUI.getInstance().dialog.getLocalToScanFolderListPanel().revalidate();
                    pLocalToScanFolderNode = pLocalToScanFolderNode.next;
        }
    }
}
